package cn.sinata.wanglin.adapter;

import android.content.DialogInterface;
import android.text.TextUtils;
import android.view.View;
import android.widget.TextView;

import com.facebook.drawee.view.SimpleDraweeView;
import com.google.gson.JsonObject;
import com.xilada.xldutils.activitys.BaseActivity;
import com.xilada.xldutils.adapter.HFRecyclerAdapter;
import com.xilada.xldutils.adapter.util.ViewHolder;
import com.xilada.xldutils.utils.DialogUtils;
import com.xilada.xldutils.utils.SharedPreferencesUtils;

import java.util.List;

import cn.sinata.wanglin.R;
import cn.sinata.wanglin.commonutils.Constant;
import cn.sinata.wanglin.model.Member;
import cn.sinata.wanglin.net.HttpManager;
import cn.sinata.wanglin.net.model.util.ResultDataSubscriber;
import cn.sinata.wanglin.utils.ImageUtil;

/**
 *
 * Created by liaoxiang on 17/1/19.
 */

public class GroupMemberAdapter extends HFRecyclerAdapter<Member> {

    private boolean isOwner = false;
    private String userId;
    private String groupAccount;
    private String groupId;
    private boolean isEdit = false;

    public GroupMemberAdapter(List<Member> mData) {
        super(mData, R.layout.item_group_member_list_layout);
        userId = SharedPreferencesUtils.getString(Constant.SP_USERID);
    }

    @Override
    public void onBind(int position, final Member member, ViewHolder holder) {
        SimpleDraweeView simpleDraweeView = holder.bind(R.id.image);
        simpleDraweeView.setImageURI(ImageUtil.parseImageUri(member.getHeadUrl()));
        holder.setText(R.id.tv_name,member.getNickName());
        TextView removeView = holder.bind(R.id.tv_remove);
        removeView.setVisibility(isOwner? View.VISIBLE:View.GONE);
        //如果是群主自己
        if (isOwner && TextUtils.equals(userId, member.getUserID())) {
            removeView.setVisibility(View.GONE);
        }
        removeView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (TextUtils.equals(userId, member.getUserID())) {
                    return;
                }
                DialogUtils.createNoticeDialog(mContext, "请注意", "您确定要移出该成员吗？", "确定", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        removeMember(member);
                    }
                });

            }
        });
    }

    public void setGroupInfo(String groupId,String groupAccount,boolean isOwner) {
        this.isOwner = isOwner;
        this.groupAccount = groupAccount;
        this.groupId = groupId;
    }

    private void removeMember(final Member member){

        if (mContext instanceof BaseActivity) {
           ((BaseActivity) mContext).showDialog();
        }

        HttpManager.removeGroupMember(member.getUserID(),groupId,groupAccount)
                .subscribe(new ResultDataSubscriber<JsonObject>((BaseActivity) mContext) {
                    @Override
                    public void onSuccess(String msg, JsonObject jsonObject) {
                        isEdit = true;
                        mData.remove(member);
                        notifyDataSetChanged();
                    }
                });
    }

    public boolean isEditMember(){
        return isEdit;
    }
}
